package com.restaurant.application.couponmanagecs;

import com.google.common.base.Strings;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.alarm.alarmclock.AlarmClock;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.commonunits.utils.SnowFlake;
import com.nebutown.generation.basicdata.StringList;
import com.nebutown.generation.basictype.UID;
import com.nebutown.generation.basictype.UIDLists;
import com.restaurant.application.coupon.Coupon;
import com.restaurant.application.coupon.support.CouponData;
import com.restaurant.application.couponmanage.CouponManage;
import com.restaurant.application.couponmanage.support.CouponManageData;
import com.restaurant.application.couponmanage.support.CreatePateTypeData;
import com.restaurant.application.couponmanagecs.support.AbstractCouponManageCS;
import com.restaurant.application.indexes.couponlist.mysql.CouponListBean;
import com.restaurant.application.indexes.couponlist.mysql.CouponListCriteria;
import com.restaurant.application.indexes.couponlist.mysql.CouponListImpl;
import com.restaurant.application.indexes.couponmanagelist.AssistCouponManageList;
import com.restaurant.application.indexes.couponmanagelist.mysql.CouponManageListBean;
import com.restaurant.application.indexes.couponmanagelist.mysql.CouponManageListImpl;
import com.restaurant.application.shoplist.ShopList;
import com.restaurant.application.util.Constant;
import com.restaurant.application.util.CouponManageUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
public class CouponManageCSImpl extends AbstractCouponManageCS {

    private String getStr(CouponManageData data) {
        if (Objects.isNull(data.getMinusMoney()) || data.getMinusMoney() <= 0 || data.getMinusMoney() >= 10) {
            return "请填写正确的折扣";
        }
        return null;
    }

    @Override
    public ViewUpdate save(CouponManageData data, int[] seq) {
        String msg = getStr(data);
        if (!Strings.isNullOrEmpty(msg)) {
            return returnViewCall(buildViewCall(unitPosition(), "showErrorMessage", null, new StringList().addData(msg)));
        }
        String massage = CouponManageUtils.getStr(data);
        if (!Strings.isNullOrEmpty(massage)) {
            return returnViewCall(buildViewCall(unitPosition(), "showErrorMessage", null, new StringList().addData(massage)));
        }

        perfect(data);

        if (Objects.isNull(getData().getCreateTime())) {
            getData()
                    .withId(SnowFlake.findUniqueId())
                    .withState(CouponManage.STATE_VALID)
                    .withCreateTime(DateTimeUtils.getCurrentMillisecond())
                    .withNumber(CouponManageUtils.getNumber(CS, this))
                    .withType(CouponManage.TYPE_REBATE)
                    .withAmount(0);

//            添加定时器
            getSingleton(AlarmClock.class).setDelayAlarmClock(getData().getId() + getData().getNumber(), (getData().getEndTime() - getData().getReceiveTime()) / 1000, getId());

            CouponManageListBean bean = getSingleton(AssistCouponManageList.class).createBean(getId(), getData());
            getIndex(CouponManageListImpl.class).insert(bean);
        }
        return returnPopFromContainer(getId(), false);
    }

    @Override
    public ViewUpdate cancel(int[] seq) {
        return returnPopFromContainer(getId(), false);
    }

    private void perfect(CouponManageData data) {
        getData().withModifyTime(DateTimeUtils.getCurrentMillisecond());
        if (!Strings.isNullOrEmpty(data.getModality())) {
            getData().withModality(data.getModality());
        }
        if (Objects.nonNull(data.getEndTime())) {
            getData().withEndTime(data.getEndTime());
        }
        if (Objects.nonNull(data.getReceiveTime())) {
            getData().withReceiveTime(data.getReceiveTime());
        }
        if (Objects.nonNull(data.getMinusMoney()) && data.getMinusMoney() > 0) {
            getData().withMinusMoney(data.getMinusMoney());
        }
        if (!Strings.isNullOrEmpty(data.getShopName())) {
            getData().withShopName(data.getShopName()).withShopUid(getSingleton(ShopList.class).findShopByName(data.getShopName()));
        }
        if (Objects.nonNull(data.getTotalAmount()) && data.getTotalAmount() > 0) {
            getData().withTotalAmount(data.getTotalAmount());
        }
        if (Objects.nonNull(data.getLimitId()) && data.getLimitId() > 0) {
            getData().withLimitId(data.getLimitId());
        }
        if (Objects.nonNull(data.getLimitAccount()) && data.getLimitAccount() > 0) {
            getData().withLimitAccount(data.getLimitAccount());
        }
        if (!Strings.isNullOrEmpty(data.getRemarks())) {
            getData().withRemarks(data.getRemarks());
        }
        if (!Strings.isNullOrEmpty(data.getState())) {
            getData().withState(data.getState());
        }
    }

    @Override
    public CouponManageListBean getCouponManageUidDataForCouponManageList(CouponManageListBean bean) {
        return getSingleton(AssistCouponManageList.class).updateBean(getId(), getData(), bean);
    }

    @Override
    public View getFullView() {
        return newCommonTable(DataModes.EDIT);
    }

    @Override
    public View getViewLead(ViewSpec viewSpec) {
        if (ViewSpec.SHORTLINE.equals(viewSpec)) {
            return newCommonTable(DataModes.SHOW);
        }
        return getFullView();
    }

    @Override
    public UIDLists batchCreateCoupon(int amount) {
        UIDLists uidLists = CouponManageUtils.batchCreateCoupon(amount, this, getData(), getId());
        if (Objects.nonNull(uidLists)) {
            int newAmount = getData().getAmount() + amount;
            getData().withAmount(newAmount);
        }
        return uidLists;
    }

    @Override
    public String createCoupon() {
        UID uid = CouponManageUtils.createCoupon(this, getData(), getId());
        if (Objects.isNull(uid)) {
            return "无法领取该优惠券!";
        }
        int amount = getData().getAmount() + 1;
        getData().withAmount(amount);
        return Constant.SUCCESS;
    }

    @Override
    public String modify(CouponManageData data) {
        perfect(data);
        return Constant.SUCCESS;
    }

    @Override
    public CouponManageData findData() {
        return getData().duplicate();
    }

    private View newCommonTable(DataModes dataModes) {
        PageForm pageForm = newFormShell(new CreatePateTypeData().withType(CouponManage.TYPE_GIVE), dataModes).getForm();
        return getSingleton(UIDataFace.class).newGenericForm(unitPosition(), pageForm);
    }

    private FormShell newFormShell(CreatePateTypeData typeData, DataModes dataModes) {
        return CouponManageUtils.newFormShell(typeData, dataModes, getData(), CMD_SAVE, CMD_CANCEL, this, CouponManage.TYPE_REBATE);
    }

    @Override
    public boolean timerCall(String s) {
        if (Objects.equals((getData().getId() + getData().getNumber()), s)) {
            getData().withState(CouponManage.STATE_INVALID);
            List<CouponListBean> beans = getIndex(CouponListImpl.class).search(new CouponListCriteria().withCouponManageUidEquals(getId().value()), null, 100000);
            if (beans.size() > 0) {
                CouponData data = new CouponData().withState(CouponManage.STATE_INVALID);
                beans.forEach(e -> getUnit(Coupon.class, e.getCouponUid()).update(data));
            }
            return false;
        }
        return true;
    }
}
